﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using T.ServiceComponent.DAC.Service;
using System.Reflection;
using System.Data;
using T.FAS.Runtime.Excel.Entity;
using T.ServiceComponent.DataAccessEx.Core;
using T.FAS.Runtime.Base.Common;

namespace T.FAS.Runtime.Excel.Dao
{
    public class ImportTaskDetailDao : DaoService<ImportTaskDetail>
    {
        public override string TableName => "FAS_ImportTaskDetail";
        public override string IDColumnName => "ID";
        public ImportTaskDetailDao() : base()
        {
            DBName = "TFAS";
        }
        /// <summary>
        /// 查询导入任务明细
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
        public List<ImportTaskDetail> GetTaskDetailList(string taskID)
        {
            var result = new List<ImportTaskDetail>();
            List<IDbDataParameter> paramList = new List<IDbDataParameter>();
            paramList.Add(DB.MakeInParam("@taskID", TDbDataType.VarChar, taskID));
            var sql = string.Format($@"select * from {TableName} where TaskID=@taskID;");
            var ds = DB.ExecuteDataSet(sql, paramList.ToArray());
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    var row = ds.Tables[0].Rows[i];
                    result.Add(ConvertToObject(Columns, row));
                }
            }
            return result.OrderBy(t => t.RowNum).ToList();
        }
        /// <summary>
        /// 查询导入任务明细
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
        public Dictionary<string, object> GetTaskDetailList(string taskID, Dictionary<string, string> filter)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            try
            {
                var result = new List<ImportTaskDetail>();
                List<IDbDataParameter> paramList = new List<IDbDataParameter>();
                paramList.Add(DB.MakeInParam("@taskID", TDbDataType.VarChar, taskID));
                var sql = $"SELECT * FROM {TableName} WHERE TaskID=@taskID and State=0 Order by RowNum";
                var sql2 = $"SELECT 1 FROM {TableName} WHERE TaskID=@taskID and State=0 ";
                var ds = DB.ExecuteDataSet(sql, paramList.ToArray());
                var totalds = DB.ExecuteDataSet(sql2, paramList.ToArray());
                if (ds != null && ds.Tables.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        result.Add(ConvertToObject(base.Columns, row));
                    }
                }
                return ToolUtil.GetTaskList(dic, result, totalds);
            }
            catch (Exception e)
            {
                dic.Add("state", "0");
                dic.Add("errmsg", e.ToString());
                return dic;
            }
        }
        /// <summary>
        /// 删除任务
        /// </summary>
        /// <param name="taskID"></param>
        /// <returns></returns>
        public int DeleteByTask(string taskID)
        {
            List<IDbDataParameter> paramList = new List<IDbDataParameter>();
            paramList.Add(DB.MakeInParam("@taskID", TDbDataType.VarChar, taskID));
            return base.DB.ExecSqlStatement($"delete from {TableName} where taskID=@taskID", paramList.ToArray());
        }
        /// <summary>
        /// 查询数据是否导入过
        /// </summary>
        /// <returns></returns>
        public int CheckIsImport(string cdoID, string uniqueID)
        {
            List<IDbDataParameter> paramList = new List<IDbDataParameter>();
            paramList.Add(DB.MakeInParam("@cdoID", TDbDataType.VarChar, cdoID));
            paramList.Add(DB.MakeInParam("@uniqueID", TDbDataType.VarChar, uniqueID));
            var sql = string.Format($@"select count(1) from {TableName} where TaskID in (select ID from FAS_ImportTask where CDOID =@cdoID) and UniqueID =@uniqueID and state = 1");
            int count;
            var result = DB.ExecuteScalar(sql, paramList.ToArray());
            if (int.TryParse(Convert.ToString(result), out count))
            {
                return count;
            }
            return 0;
        }
    }
}
